package se137.hrbust.hlgshopping137.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import se137.hrbust.hlgshopping137.entity.Cart137;
import se137.hrbust.hlgshopping137.entity.Product137;
import se137.hrbust.hlgshopping137.entity.Prosku137;
import se137.hrbust.hlgshopping137.mapper.Product137Mapper;
import se137.hrbust.hlgshopping137.mapper.Prosku137Mapper;
import se137.hrbust.hlgshopping137.service.Cart137Service;
import se137.hrbust.hlgshopping137.mapper.Cart137Mapper;
import org.springframework.stereotype.Service;
import se137.hrbust.hlgshopping137.utils.Result137;
import se137.hrbust.hlgshopping137.utils.SQLCommService137;

import java.math.BigDecimal;

/**
* @author ZhaoSY
* @description 针对表【tb_cart137(购物车表)】的数据库操作Service实现
* @createDate 2025-09-11 09:43:29
*/
@Service
public class Cart137ServiceImpl extends ServiceImpl<Cart137Mapper, Cart137>
    implements Cart137Service{
   @Autowired
  Prosku137Mapper proskuMapper;
   @Autowired
   Product137Mapper productMapper;
    @Autowired
    private Cart137Mapper cart137Mapper;
    @Autowired
    SQLCommService137 sqlCommService137;


    @Override
    public boolean addCart(Long skuid, int pnum) {
        Cart137 cart137 = new Cart137();
        Prosku137 prosku = proskuMapper.selectById(skuid);

        Long pid = prosku.getPid();
        Product137 product137 = productMapper.selectById(pid);

        cart137.setUserid(1L);
        cart137.setProductid(pid);
        cart137.setSkuid(skuid);
        System.out.println(product137);
        cart137.setName137(product137.getPname137());
        cart137.setPicture137(product137.getPimage1());
        cart137.setPrice(BigDecimal.valueOf(prosku.getPricesell137()/100));
        cart137.setNowprice(BigDecimal.valueOf(prosku.getPricesell137()/100));
        cart137.setNoworiginalprice(BigDecimal.valueOf(prosku.getPriceorigion()/100));
        cart137.setStock(prosku.getStocknum());

        Cart137 cartOri = cart137Mapper.findCartByProSkuId(pid,skuid);

        if(cartOri == null){
            cart137.setCount(pnum);
            return cart137Mapper.insert(cart137)==1;


        }else {
            cartOri.setCount(cartOri.getCount()+pnum);
            return cart137Mapper.updateById(cartOri)==1;
        }



    }

    @Override
    public Result137 ListCart() {
        QueryWrapper<Cart137> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("FUserId",1L);
       return new Result137<>().ok(cart137Mapper.selectList(queryWrapper));
    }
    @Override
    public Result137 delCart(Long id) {
       int ret=cart137Mapper.deleteById(id);
       if (ret==1) return new Result137<>().ok("删除成功");
       return new Result137<>().fail("删除失败");
    }
    @Override
    public Result137 updateCartNum(Long cid, int num) {
       String sql = "update tb_cart137 set FCount = "+num+" where FId = "+cid;
       int ret = sqlCommService137.updateSQL(sql);
       if (ret==1) return new Result137<>().ok("修改成功");
       return new Result137<>().fail("修改失败");
    }
}




